package com.lwl.office.webeditor.style;

import cn.hutool.core.util.StrUtil;
import com.lwl.office.webeditor.tools.ConvertUtil;
import lombok.Data;
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.math.BigInteger;
import java.util.Map;

@Data
public class RStyle extends Style{
    //外边框
    private String border;
    //文本修饰：下划线（underline）、上划线（overline）、删除线（line-through）
    private String textDecoration;
    //字体大小
    private String fontSize;
    //字体
    private String fontFamily;
    //字体颜色
    private String color;
    //背景颜色
    private String bgColor;
    //加粗
    private Boolean bold;
    //斜体
    private Boolean italic;
    //上标
    private Boolean superscript;
    //下标
    private Boolean subscript;

    public RStyle of(String style){
        Map<String, String> styleMap = parseStyle(style);
        border = styleMap.get("border");
        textDecoration = styleMap.get("text-decoration");
        fontSize = styleMap.get("font-size");
        fontFamily = styleMap.get("font-family");
        if(StrUtil.isNotEmpty(fontFamily) && fontFamily.contains(",")){
            fontFamily = fontFamily.substring(0,fontFamily.indexOf(","));
        }
        color = styleMap.get("color");
        bgColor = styleMap.get("background-color");
        return this;
    }
    public void rend(CTRPr rpr){
        //外边框
        if(StrUtil.isNotEmpty(border)){
            CTBorder bdr = rpr.addNewBdr();
            bdr.setVal(STBorder.SINGLE);
            bdr.setSz(BigInteger.valueOf(4));
            bdr.setSpace(BigInteger.valueOf(0));
            bdr.setColor("auto");
        }
        //文本修饰
        if(StrUtil.isNotEmpty(textDecoration)){
            if(textDecoration.equals("underline")){
                rpr.addNewU();
            }else if(textDecoration.equals("line-through")){
                rpr.addNewStrike();
            }
        }
        //字体大小
        if(StrUtil.isNotEmpty(fontSize)){
            CTHpsMeasure sz = rpr.addNewSz();
            sz.setVal(ConvertUtil.toWordFontSize(fontSize));
            CTHpsMeasure szCs = rpr.addNewSzCs();
            szCs.setVal(sz.getVal());
        }
        //字体
        if(StrUtil.isNotEmpty(fontFamily)){
            CTFonts rFonts = rpr.addNewRFonts();
            rFonts.setAscii(fontFamily);
            rFonts.setEastAsia(fontFamily);
//            rFonts.setHAnsi(fontFamily);
//            rFonts.setHint(STHint.EAST_ASIA);
        }
        //颜色
        if(StrUtil.isNotEmpty(color)){
            CTColor color = rpr.addNewColor();
            color.setVal(ConvertUtil.toHexColor(this.color));
        }
        //背景色
        if(StrUtil.isNotEmpty(bgColor)){
            CTShd shd = rpr.addNewShd();
            shd.setVal(STShd.CLEAR);
            shd.setColor("auto");
            shd.setFill(ConvertUtil.toHexColor(this.bgColor));
        }
        //加粗
        if(bold!=null && bold){
            rpr.addNewB();
            rpr.addNewBCs();
        }
        //斜体
        if(italic!=null && italic){
            rpr.addNewI();
            rpr.addNewICs();
        }
        //上标
        if(superscript!=null && superscript){
            CTVerticalAlignRun vertAlign = rpr.addNewVertAlign();
            vertAlign.setVal(STVerticalAlignRun.SUPERSCRIPT);
        }
        //下标
        if(subscript!=null && subscript){
            CTVerticalAlignRun vertAlign = rpr.addNewVertAlign();
            vertAlign.setVal(STVerticalAlignRun.SUBSCRIPT);
        }
    }
}
